<template>
  <div class="user-login-container">
    <div class="login-background">
      <div class="background-overlay"></div>
    </div>
    
    <div class="login-card">
      <div class="card-header">
        <div class="logo-section">
          <div class="user-icon">
            <i class="el-icon-user"></i>
          </div>
          <h1 class="title">欢迎登录</h1>
          <p class="subtitle">Welcome to Login</p>
        </div>
      </div>
      
      <div class="card-body">
      <el-form
          :model="form"
            :rules="rules" 
            ref="loginForm"
          label-width="0px"
          class="login_form">
          
          <div class="form-group">
            <label class="form-label">
              <i class="el-icon-user"></i>
              用户名
            </label>
          <el-input
              v-model="form.username"
              placeholder="请输入用户名"
                class="form-input"
                prefix-icon="el-icon-user">
            </el-input>
          </div>
          
          <div class="form-group">
            <label class="form-label">
              <i class="el-icon-key"></i>
              密码
            </label>
            <el-input
                v-model="form.password"
                placeholder="请输入密码"
                type="password"
                class="form-input"
                prefix-icon="el-icon-key">
            </el-input>
          </div>
          
          <div class="form-group">
            <label class="form-label">
              <i class="el-icon-lock"></i>
              验证码
            </label>
            <div class="captcha-container">
              <el-input
                  v-model="form.validCode"
                  placeholder="请输入验证码"
                  class="captcha-input"
                  prefix-icon="el-icon-lock"
                  @keydown.enter.native="login">
              </el-input>
              <ValidCode 
                  @input="getValidCode" 
                  ref="child" 
                  class="captcha-code unselectable">
              </ValidCode>
            </div>
          </div>
          
          <div class="button-group">
            <button @click="login()" class="btn btn-primary" :disabled="loading">
              <i class="el-icon-right"></i>
              {{ loading ? '登录中...' : '登录' }}
            </button>
            <button @click="$router.push('/register')" class="btn btn-secondary">
              <i class="el-icon-plus"></i>
              注册
            </button>
            <button @click="jump()" class="btn btn-admin">
              <i class="el-icon-s-custom"></i>
              管理员登录
            </button>
          </div>
          
      </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import ValidCode from "@/components/ValidCode.vue";
import request from "@/utils/request";

export default {
  name: "Login",
  created() {
    document.title = '欢迎登录';
  },
  components:{
    ValidCode,
  },
  data() {
    return {
      form: {
        username:'',
        password: '',
        validCode: ''
      },
      validCode: "",
      loading: false,
      // 表单数据验证规则
      rules: {
        username: [
          { required: true, message: "请输入用户名！", trigger: "blur" },
          {
            min: 2,
            max: 25,
            message: "长度在 2 到 25 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码！", trigger: "blur" },
          { min: 1, max: 15, message: "长度在 1到 15 个字符", trigger: "blur" },
        ],
        validCode: [
          { required: true, message: "请输入验证码！", trigger: "blur" }
        ],
      },
    }
  },
  methods: {
    // 从验证码子组件里面获取正确的验证码
    getValidCode(data){
      this.validCode = data;
    },
    login() {
      this.$refs['loginForm'].validate((valid) => {
        // 如果满足输入的规则
        if (valid){
          // 验证码不正确
          if (this.form.validCode.toLowerCase() !== this.validCode.toLowerCase()){
            this.$message.error('验证码错误');
            // 更新验证码
            this.$refs.child.refreshCode();
            return;
          }
          
          this.loading = true;
          
          // 出来了就是验证码正确，就发送给后端
          request.post('/user/login', this.form).then(res => {
            console.log('登录响应:', res);
            if (res.code === '0') {
              this.$message({
                type: 'success',
                message: "登录成功",
                duration: 1500
              });
              // 缓存用户信息
              sessionStorage.setItem("user", JSON.stringify(res.data));
              console.log('用户信息已保存:', res.data);
              
              // 立即跳转 - 使用多种方法确保跳转成功
              setTimeout(() => {
                // 方法1: 使用Vue Router
                this.$router.push('/shop').then(() => {
                  console.log('Vue Router跳转成功');
                }).catch(err => {
                  console.error('Vue Router跳转失败:', err);
                  // 方法2: 使用replace
                  this.$router.replace('/shop').then(() => {
                    console.log('Vue Router replace跳转成功');
                  }).catch(replaceErr => {
                    console.error('Vue Router replace跳转失败:', replaceErr);
                    // 方法3: 直接修改location
                    console.log('使用window.location跳转');
                    window.location.href = '/shop';
                  });
                });
              }, 100);
            } else {
              console.log('登录失败:', res.msg);
              this.$message({
                type: 'error',
                message: res.msg,
                duration: 3000
              })
            }
          }).catch(error => {
            console.error('登录请求失败:', error);
            this.$message.error('登录失败，请检查网络连接');
          }).finally(() => {
            this.loading = false;
          });
        }
      });
    },
    jump(){
      this.$router.push('/manageLogin');
    }
  }
}
</script>

<style scoped>
/* 全局容器 */
.user-login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

/* 背景设计 */
.login-background {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: url("@/assets/images/login.jpg") no-repeat;
  background-size: cover;
  z-index: 1;
}

.background-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.4);
}

/* 登录卡片 */
.login-card {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border-radius: 24px;
  box-shadow: 
    0 20px 40px rgba(0, 0, 0, 0.15),
    0 0 0 1px rgba(255, 255, 255, 0.2);
  width: 480px;
  max-width: 90vw;
  position: relative;
  z-index: 2;
  overflow: hidden;
  animation: slideUp 0.6s ease-out;
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 卡片头部 */
.card-header {
  background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
  padding: 40px 30px 30px;
  text-align: center;
  position: relative;
}

.card-header::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><defs><pattern id="grain" width="100" height="100" patternUnits="userSpaceOnUse"><circle cx="25" cy="25" r="1" fill="white" opacity="0.1"/><circle cx="75" cy="75" r="1" fill="white" opacity="0.1"/><circle cx="50" cy="10" r="0.5" fill="white" opacity="0.1"/></pattern></defs><rect width="100" height="100" fill="url(%23grain)"/></svg>');
  opacity: 0.3;
}

.logo-section {
  position: relative;
  z-index: 1;
}

.user-icon {
  width: 80px;
  height: 80px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 20px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  backdrop-filter: blur(10px);
}

.user-icon i {
  font-size: 36px;
  color: white;
}

.title {
  color: white;
  font-size: 28px;
  font-weight: 600;
  margin: 0 0 8px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.subtitle {
  color: rgba(255, 255, 255, 0.8);
  font-size: 14px;
  margin: 0;
  font-weight: 300;
  letter-spacing: 1px;
}

/* 卡片主体 */
.card-body {
  padding: 40px 30px;
}

/* 表单组 */
.form-group {
  margin-bottom: 32px;
}

.form-label {
  display: flex;
  align-items: center;
  color: #4a5568;
  font-size: 15px;
  font-weight: 500;
  margin-bottom: 12px;
}

.form-label i {
  margin-right: 8px;
  color: #4f46e5;
  font-size: 16px;
}

/* Element UI 输入框样式覆盖 */
.form-input >>> .el-input__inner {
  width: 100%;
  padding: 20px 24px;
  border: 2px solid #e2e8f0;
  border-radius: 12px;
  font-size: 16px;
  transition: all 0.3s ease;
  background: #f8fafc;
  box-sizing: border-box;
  line-height: 1.5;
}

.form-input >>> .el-input__inner:focus {
  outline: none;
  border-color: #4f46e5;
  background: white;
  box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1);
  transform: translateY(-1px);
}

.form-input >>> .el-input__inner::placeholder {
  color: #a0aec0;
}

/* 验证码容器 */
.captcha-container {
  display: flex;
  gap: 12px;
  align-items: flex-end;
}

.captcha-input {
  flex: 1;
}

.captcha-input >>> .el-input__inner {
  width: 100%;
  padding: 20px 24px;
  border: 2px solid #e2e8f0;
  border-radius: 12px;
  font-size: 16px;
  transition: all 0.3s ease;
  background: #f8fafc;
  box-sizing: border-box;
  line-height: 1.5;
}

.captcha-input >>> .el-input__inner:focus {
  outline: none;
  border-color: #4f46e5;
  background: white;
  box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1);
  transform: translateY(-1px);
}

.captcha-code {
  flex-shrink: 0;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease;
}

.captcha-code:hover {
  transform: translateY(-2px);
}

/* 按钮组 */
.button-group {
  display: flex;
  gap: 12px;
  margin: 40px 0 20px 0;
}

.btn {
  flex: 1;
  padding: 16px 12px;
  border: none;
  border-radius: 12px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  position: relative;
  overflow: hidden;
  white-space: nowrap;
  min-width: 0;
}

.btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: left 0.5s;
}

.btn:hover::before {
  left: 100%;
}

.btn-primary {
  background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
  color: white;
  box-shadow: 0 4px 15px rgba(79, 70, 229, 0.4);
}

.btn-primary:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(79, 70, 229, 0.6);
}

.btn-secondary {
  background: #f7fafc;
  color: #4a5568;
  border: 2px solid #e2e8f0;
}

.btn-secondary:hover {
  background: #edf2f7;
  border-color: #cbd5e0;
  transform: translateY(-1px);
}

.btn-admin {
  background: linear-gradient(135deg, #059669 0%, #10b981 100%);
  color: white;
  box-shadow: 0 4px 15px rgba(5, 150, 105, 0.4);
}

.btn-admin:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(5, 150, 105, 0.6);
}

.btn:active {
  transform: translateY(0);
}

.btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none !important;
}


/* 不可选择文本 */
.unselectable {
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-card {
    width: 95vw;
    margin: 20px;
  }
  
  .card-header {
    padding: 30px 20px 20px;
  }
  
  .card-body {
    padding: 30px 20px;
  }
  
  .title {
    font-size: 24px;
  }
  
  .button-group {
    flex-direction: column;
  }
  
  .captcha-container {
    flex-direction: column;
    gap: 8px;
  }
  
  .captcha-code {
    align-self: center;
  }
}

/* 动画效果 */
.form-group {
  animation: fadeInUp 0.6s ease-out;
  animation-fill-mode: both;
}

.form-group:nth-child(1) { animation-delay: 0.1s; }
.form-group:nth-child(2) { animation-delay: 0.2s; }
.form-group:nth-child(3) { animation-delay: 0.3s; }
.button-group { animation: fadeInUp 0.6s ease-out 0.4s both; }

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* Element UI 表单样式调整 */
.login_form >>> .el-form-item {
  margin-bottom: 0;
}

.login_form >>> .el-form-item__error {
  color: #e53e3e;
  font-size: 12px;
  margin-top: 4px;
}
</style>
